<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui"
	template="/templates/template.xhtml">

	<f:metadata>
		<f:event type="preRenderView" listener="#{entradaBean.verificarAcesso}" />
	</f:metadata>

	<ui:define name="body">

		<h:form id="form" prependId="false">
			<p:fieldset legend="#{entradaBean.qualifiedName}" id="fieldPrincipal">
				<h:panelGrid columns="4" id="panelCadastro" columnClasses="coluna15,coluna30,coluna20,coluna45" width="100%">

					<h:outputLabel value="Nota Fiscal:" styleClass="icoObrigatorio" />
					<p:inputText value="#{entradaBean.model.notaFiscal}" requiredMessage="#{entradaBean.requiredMessage}"
						required="true" id="nf" size="50" maxlength="255" />
					<p:message for="nf" />
					<h:outputText value=" " />

					<h:outputLabel value="Fornecedor:" styleClass="icoObrigatorio" />
					<p:autoComplete queryDelay="500" value="#{entradaBean.fornecedor}" minQueryLength="2" id="fornecedor" size="50"
						completeMethod="#{entradaBean.completeFornecedor}" forceSelection="true" var="fornecedor"
						itemLabel="#{fornecedor.razaoSocial}" itemValue="#{fornecedor.id}" converter="fornecedorConverter"
						requiredMessage="#{entradaBean.requiredMessage}" required="true" />
					<p:message for="fornecedor" />
					<h:outputText value=" " />

					<h:outputText value="Data de Entrada:" styleClass="icoObrigatorio" />
					<h:panelGrid columns="2" cellpadding="0" cellspacing="0">
						<p:calendar locale="pt" pattern="dd/MM/yyyy" id="dtEntrada" navigator="true" maxlength="10" size="15"
							readOnlyInputText="true" value="${entradaBean.model.dtEntrada}" mode="popup" showOn="button"
							requiredMessage="#{entradaBean.requiredMessage}" required="true" />
						<p:commandLink process="@this" title="Limpar" update="dtEntrada">
							<h:graphicImage name="icone-limpar.png" library="design/imagem" />
							<f:setPropertyActionListener target="${entradaBean.model.dtEntrada}" value="" />
						</p:commandLink>
					</h:panelGrid>
					<p:message for="dtEntrada" />
					<h:outputText value=" " />

					<h:outputText value="Data do Pedido:" styleClass="icoObrigatorio" />
					<h:panelGrid columns="2" cellpadding="0" cellspacing="0">
						<p:calendar locale="pt" pattern="dd/MM/yyyy" id="dtPedido" navigator="true" maxlength="10" size="15"
							readOnlyInputText="true" value="${entradaBean.model.dtPedido}" mode="popup" showOn="button"
							requiredMessage="#{entradaBean.requiredMessage}" required="true" />
						<p:commandLink process="@this" title="Limpar" update="dtPedido">
							<h:graphicImage name="icone-limpar.png" library="design/imagem" />
							<f:setPropertyActionListener target="${entradaBean.model.dtPedido}" value="" />
						</p:commandLink>
					</h:panelGrid>
					<p:message for="dtPedido" />
					<h:outputText value=" " />

					<h:outputLabel value="Valor Frete:" styleClass="icoObrigatorio" />
					<p:inputText value="#{entradaBean.model.valorFreteToString}" id="frete" size="20" maxlength="7"
						requiredMessage="#{entradaBean.requiredMessage}" required="true" />
					<p:message for="frete" />
					<h:outputText value=" " />

					<h:outputLabel value="Valor Total:" styleClass="icoObrigatorio" />
					<p:inputText value="#{entradaBean.model.valorTotalToString}" id="valorTotal" size="20" maxlength="7"
						requiredMessage="#{entradaBean.requiredMessage}" required="true" />
					<p:message for="valorTotal" />
					<h:outputText value=" " />

				</h:panelGrid>
				<p:fieldset legend="Item de Entrada" id="fieldItem">
					<h:panelGrid columns="2" id="panelItem" columnClasses="coluna15,coluna85" width="100%">
						<h:outputLabel value="Produto:" styleClass="icoObrigatorio" />
						<p:autoComplete queryDelay="500" value="#{entradaBean.produto}" minQueryLength="2" id="produto" size="50"
							completeMethod="#{entradaBean.completeProduto}" forceSelection="true" var="produto"
							itemLabel="#{produto.descricao}" itemValue="#{produto.id}" converter="produtoConverter" />

						<h:outputLabel value="Marca:" styleClass="icoObrigatorio" />
						<p:autoComplete queryDelay="500" value="#{entradaBean.marca}" minQueryLength="2" id="marca" size="50"
							completeMethod="#{entradaBean.completeMarca}" forceSelection="true" var="marca" itemLabel="#{marca.descricao}"
							itemValue="#{marca.id}" converter="marcaConverter" />


						<h:outputLabel value="Quantidade:" styleClass="icoObrigatorio" />
						<p:inputText value="#{entradaBean.itemEntrada.quantidadeToString}" id="qtd" size="20" maxlength="7" />

						<h:outputLabel value="Valor:" styleClass="icoObrigatorio" />
						<p:inputText value="#{entradaBean.itemEntrada.valorToString}" id="valor" size="20" maxlength="7" />

						<h:outputLabel value="Porcentagem Lucro (%):" styleClass="icoObrigatorio" />
						<p:inputText value="#{entradaBean.itemEntrada.porcetagemLucroToString}" id="lucro" size="20" maxlength="7">
						</p:inputText>

						<h:outputLabel value="Valor Final:" styleClass="icoObrigatorio" />
						<p:inputText value="#{entradaBean.itemEntrada.valorFinalToString}" id="valorFinal" size="20" maxlength="7" />

						<h:outputText value="Data de Validade:" styleClass="icoObrigatorio" />
						<h:panelGrid columns="2" cellpadding="0" cellspacing="0">
							<p:calendar locale="pt" pattern="dd/MM/yyyy" id="dtValidade" navigator="true" maxlength="10" size="15"
								readOnlyInputText="true" value="${entradaBean.itemEntrada.dtValidade}" mode="popup" showOn="button" />
							<p:commandLink process="@this" title="Limpar" update="dtValidade">
								<h:graphicImage name="icone-limpar.png" library="design/imagem" />
								<f:setPropertyActionListener target="${entradaBean.itemEntrada.dtValidade}" value="" />
							</p:commandLink>
						</h:panelGrid>

					</h:panelGrid>
					<p:toolbar>
						<p:toolbarGroup align="left">

							<p:commandButton value="Limpar" ajax="true" process="fieldItem" update="fieldItem"
								action="#{entradaBean.limparItem}" />
							<p:commandButton value="Adicionar" ajax="true" process="fieldItem" update="fieldItem"
								action="#{entradaBean.adicionarItem}" />
						</p:toolbarGroup>
					</p:toolbar>
					<br />
					<p:dataTable var="lista" id="lista" emptyMessage="#{entradaBean.emptyMessage}" update="lista"
						rowsPerPageTemplate="#{entradaBean.paginacao}" styleClass="coluna100" value="#{entradaBean.itens}"
						paginator="true" rows="5" rowStyleClass="#{empty rowIx or rowIx mod 2 ne 0 ? 'linhaImpar' : 'linhaPar'}"
						rowIndexVar="rowIx">

						<p:column headerText="Produto" sortBy="#{lista.produto.descricao}" filterBy="#{lista.produto.descricao}"
							style="width:20%">
							<h:outputLabel value="#{lista.produto.descricao}" />
						</p:column>
						<p:column headerText="Marca" sortBy="#{lista.marca.descricao}" filterBy="#{lista.marca.descricao}"
							style="width:20%">
							<h:outputLabel value="#{lista.marca.descricao}" />
						</p:column>
						<p:column headerText="Qtd" style="width:10%">
							<h:outputLabel value="#{lista.quantidade}" />
						</p:column>
						<p:column headerText="Valor" style="width:10%">
							<h:outputLabel value="#{lista.valor}" />
						</p:column>
						<p:column headerText="% Lucro" style="width:10%">
							<h:outputLabel value="#{lista.porcetagemLucro}" />
						</p:column>
						<p:column headerText="Valor Final" style="width:10%">
							<h:outputLabel value="#{lista.valorFinal}" />
						</p:column>
						<p:column headerText="Validade" sortBy="#{lista.dtValidade}" style="width:10%">
							<h:outputLabel value="#{lista.dtValidade}">
								<f:convertDateTime pattern="dd/MM/yyyy" />
							</h:outputLabel>
						</p:column>
						<p:column headerText="Ações" style="width:10%;text-align:center;">

							<p:commandLink rendered="#{p:ifGranted('ROLE_USER')}" title="Editar" style="padding:5px !important;"
								update=":form:panelItem" process="@this" action="#{entradaBean.loadItem}">
								<h:graphicImage library="design/imagem" name="icone-editar.png" />
								<f:setPropertyActionListener target="#{entradaBean.itemEntrada}" value="#{lista}" />
							</p:commandLink>

							<p:commandLink rendered="#{p:ifGranted('ROLE_USER')}" title="Excluir" process="@this"
								oncomplete="confirmationMedicao.show()">
								<h:graphicImage library="design/imagem" name="icone-excluir.png" />
								<f:setPropertyActionListener target="#{entradaBean.itemEntradaExcluir}" value="#{lista}" />
							</p:commandLink>
						</p:column>

					</p:dataTable>

				</p:fieldset>

				<br />
				<p:toolbar>
					<p:toolbarGroup align="left">
						<p:commandButton value="Limpar" immediate="true" ajax="false" action="entrada.jsf" />
						<p:commandButton rendered="#{p:ifGranted('ROLE_USER')}" value="Salvar" ajax="true" process="@form" update="@form"
							action="#{entradaBean.save}" />
					</p:toolbarGroup>
				</p:toolbar>
				<br />
			</p:fieldset>

			<p:confirmDialog appendToBody="true" message="Confirma a Exclusão" header="Exclusão" severity="alert"
				widgetVar="confirmationMedicao">
				<h:panelGrid columns="2">
					<p:commandButton value="Sim" update="lista" oncomplete="confirmationMedicao.hide()"
						actionListener="#{entradaBean.deleteItem}" immediate="true" />
					<p:commandButton value="Não" onclick="confirmationMedicao.hide()" />
				</h:panelGrid>
			</p:confirmDialog>
		</h:form>
	</ui:define>
</ui:composition>